import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/convenient/visitor/record_list_controller.dart';
import 'package:kkhousekeep/widget/custom_app_bar.dart';
import 'package:kkhousekeep/widget/custom_button.dart';

import '../../../../models/home_visitor_record_entity.dart';

class BuildVisitorRecordPage extends StatelessWidget {
  const BuildVisitorRecordPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: VisitorRecordPageController(),
      builder: (controller) => Scaffold(
          appBar: CustomAppBar(
            "访客记录",
            isCenterTitle: true,
          ),
          body: Stack(
              children: [
                Obx(
                  () => ConstrainedBox(
                    constraints: const BoxConstraints.expand(),
                    child: Container(
                      color: const Color(0xFFF4F5F6),
                      padding:
                          EdgeInsets.only(left: 32.w, right: 32.w, top: 32.h),
                      child: ListView.builder(
                        itemCount: controller.visitorRecordList.length,
                        itemExtent: 384.h,
                        itemBuilder: (context, index) => _buildSingleItem(
                            controller.visitorRecordList[index],
                            controller,
                            index),
                      ),
                    ),
                  ),
                ),
                Positioned(
                  bottom: 102.h,
                  left: 166.w,
                  child: _buildButton(controller),
                ),
              ],
            ),
          ),
    );
  }

  Widget _buildSingleItem(HomeVisitorRecordObjList visitorInformation,
      VisitorRecordPageController controller, int index) {
    return Column(
      children: [
        InkWell(
          onTap: () => controller.buildVisitorDetail(index),
          child: Container(
            padding: EdgeInsets.only(left: 24.w, right: 24.w,bottom: 28.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(16.w)),
              color: Colors.white,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildApplicationTimeWidget(visitorInformation),
                SizedBox(
                  height: 26.h,
                ),
                _buildVisitorInformationWidget(visitorInformation),
                SizedBox(
                  height: 20.h,
                ),
                _buildTimeWidget(visitorInformation)
              ],
            ),
          ),
        ),
        SizedBox(
          height: 16.h,
        )
      ],
    );
  }

  Widget _buildApplicationTimeWidget(
      HomeVisitorRecordObjList visitorInformation) {
    return Container(
      padding: EdgeInsets.only(top: 10.h, bottom: 10.h),
      decoration: const BoxDecoration(
        border:
            Border(bottom: BorderSide(width: 1.0, color: Color(0xFFF4F5F6))),
      ),
      child: _buildText(
          visitorInformation.createTime
              .substring(0, visitorInformation.createTime.lastIndexOf("+") - 1),
          TextState.date),
    );
  }

  Widget _buildVisitorInformationWidget(
      HomeVisitorRecordObjList visitorInformation) {
    return Column(
      children: [
        Flex(
          direction: Axis.horizontal,
          children: [
            Expanded(
              flex: 3,
              child: Container(
                  alignment: Alignment.centerLeft,
                  child: _buildText("访客姓名", TextState.informationAttribute)),
            ),
            Expanded(
                flex: 7,
                child: Container(
                  alignment: Alignment.centerRight,
                  child: _buildText(
                      visitorInformation.visitor, TextState.information),
                ))
          ],
        ),
        SizedBox(
          height: 20.h,
        ),
        Flex(
          direction: Axis.horizontal,
          children: [
            Expanded(
              flex: 3,
              child: Container(
                  alignment: Alignment.centerLeft,
                  child: _buildText("到访地址", TextState.informationAttribute)),
            ),
            Expanded(
                flex: 7,
                child: Container(
                  alignment: Alignment.centerRight,
                  child: _buildText("12栋101号", TextState.information),
                ))
          ],
        ),
      ],
    );
  }

  Widget _buildTimeWidget(HomeVisitorRecordObjList visitorInformation) {
    return Container(
      padding: EdgeInsets.only(left: 24.w, top: 24.h, bottom: 24.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(16.w)),
        color: const Color(0xFF1774E0).withOpacity(0.1),
      ),
      child: Row(
        children: [
          Image.asset(
            "assets/images/home_visitor_time_icon.png",
            height: 80.h,
            width: 20.w,
          ),
          SizedBox(
            width: 16.w,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  _buildText("到访时间", TextState.dateAttribute),
                  SizedBox(
                    width: 32.w,
                  ),
                  _buildText(
                      visitorInformation.visitStartTime.substring(
                        0,
                        visitorInformation.visitStartTime.lastIndexOf("+") - 1,
                      ),
                      TextState.dateAttribute),
                ],
              ),
              SizedBox(
                height: 24.h,
              ),
              Row(
                children: [
                  _buildText("离开时间", TextState.dateAttribute),
                  SizedBox(
                    width: 32.w,
                  ),
                  _buildText(
                      visitorInformation.visitEndTime.substring(
                        0,
                        visitorInformation.visitEndTime.lastIndexOf("+") - 1,
                      ),
                      TextState.dateAttribute),
                ],
              ),
              SizedBox(
                width: 32.w,
              ),
            ],
          )
        ],
      ),
    );
  }

  Widget _buildButton(VisitorRecordPageController controller) {
    return CustomButton(
      text: "+ 新增申请",
      width: 420.w,
      height: 90.h,
      isAsh: false,
      onPressed: () => controller.buildVisitorPassPage(),
    );
  }

  Widget _buildText(String context, TextState textState) {
    if (textState == TextState.date) {
      return Text(
        context,
        style: TextStyle(fontSize: 24.sp, color: const Color(0xFF999999)),
      );
    }
    if (textState == TextState.information) {
      return Text(
        context,
        style: TextStyle(fontSize: 28.sp, color: const Color(0xFF333333)),
      );
    }
    if (textState == TextState.informationAttribute) {
      return Text(
        context,
        style: TextStyle(fontSize: 28.sp, color: const Color(0xFF6D6D6D)),
      );
    }
    return Text(
      context,
      style: TextStyle(fontSize: 24.sp, color: const Color(0xFF6D6D6D)),
    );
  }
}

enum TextState {
  dateAttribute,
  date,
  informationAttribute,
  information,
}
